import { defineStore } from 'pinia'
import api from '@/api'
import { storeInfo } from './storeinfo';
const storeInfoStore = storeInfo();

export const studentClass = defineStore('studentClass', {
    state: () => ({
        classinfoList: [] as Array<{
            id: string;
            stage: number;
            classBaseinfoDTO: {
                classId: string;
                major: string;
                termNumber: number;
            };
            createdTime:string;
        }>,
        classId : String
    }),
    actions: {
        async getStudentClassById(userId: string) {
            // 调用API获取教师班级信息
            await api.studentClass.getStudentClassById(userId).then(res => {
                console.log("学生班级信息：",res);
                this.classinfoList = res as any
                this.classId = res[0].classBaseinfoDTO.classId
                //存入pinia
                storeInfoStore.setStudentClassId(res[0].classBaseinfoDTO.classId);
            }).catch(error => {
                console.error("获取学生班级信息失败 :", error);
                throw error;
            });
        },
    },
    getters: {
        // 返回组合后的班级信息数组
        studentClassInfo(): Array<{
            id: string;
            className: string;
            stages: number[];
        }> {
            const classMap = new Map<string, {
                id: string;
                className: string;
                stages: number[];
                // createdTime: Date;
            }>();
            this.classinfoList.forEach(item => {
                const key = `${item.classBaseinfoDTO.major}第${item.classBaseinfoDTO.termNumber}期`;//班级的输出格式
                if (!classMap.has(key)) {
                    classMap.set(key, {
                        id: item.classBaseinfoDTO.classId,
                        className: key,
                        stages: []
                        // createdTime:
                    });
                }
                classMap.get(key)!.stages.push(item.stage);
            });
                return Array.from(classMap.values());
        }
    }
});